package com.zimi.service;

import com.zimi.bean.Column;
import com.zimi.mapper.WfFormPluginsMapper;
import com.zimi.mapper.WfModuleMapper;
import com.zimi.model.WfFormPlugins;
import com.zimi.model.WfFormPluginsVO;
import com.zimi.model.WfModule;
import com.zimi.util.DbUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;

/**
 * @Auther: lmc
 * @Date: 2019/3/27 10:33
 * @Description:
 */
@Service
public class WfFormPluginsService {

    @Autowired
    private WfFormPluginsMapper wfFormPluginsMapper;
    @Autowired
    private WfModuleMapper wfModuleMapper;
    @Autowired
    private DataSource dataSource;


    public int deleteByPrimaryKey(Integer id) {
        return wfFormPluginsMapper.deleteByPrimaryKey(id);
    }


    public int insert(WfFormPlugins wfFormPlugins) {
        return wfFormPluginsMapper.insert(wfFormPlugins);
    }


    public int insertSelective(WfFormPlugins wfFormPlugins) {
        WfFormPlugins formPlugins = new WfFormPlugins();
        if (wfFormPlugins != null) {
            formPlugins.setModuleId(wfFormPlugins.getModuleId());
        }

        Integer maxOrderNum = wfFormPluginsMapper.selectMaxOrderNum(formPlugins);
        if (maxOrderNum == null) {
            maxOrderNum = 1;
        } else {
            maxOrderNum = maxOrderNum + 1;
        }

        wfFormPlugins.setOrderNum(maxOrderNum);

        wfFormPlugins.setHtmlContent(getHTMLContent(wfFormPlugins));

        return wfFormPluginsMapper.insertSelective(wfFormPlugins);
    }


    public WfFormPlugins selectByPrimaryKey(Integer id) {
        return wfFormPluginsMapper.selectByPrimaryKey(id);
    }


    public WfFormPlugins selectVOById(Integer id) {
        return wfFormPluginsMapper.selectVOById(id);
    }


    public int updateByPrimaryKeySelective(WfFormPlugins wfFormPlugins) {
        return wfFormPluginsMapper.updateByPrimaryKeySelective(wfFormPlugins);
    }


    public int updateByPrimaryKey(WfFormPlugins wfFormPlugins) {
        return wfFormPluginsMapper.updateByPrimaryKey(wfFormPlugins);
    }


    public List<WfFormPlugins> selectAll(WfFormPlugins wfFormPlugins) {
        return wfFormPluginsMapper.selectAll(wfFormPlugins);
    }

    public List<WfFormPluginsVO> selectVOAll(WfFormPlugins wfFormPlugins) {
        return wfFormPluginsMapper.selectVOAll(wfFormPlugins);
    }

    public String saveInputDbInfo(Integer moduleId) {
        WfModule dbTable = wfModuleMapper.selectByPrimaryKey(moduleId);

        try {
            Connection connDb = this.dataSource.getConnection();
            List<Column> list = DbUtil.getTableColumns(connDb, connDb.getCatalog(), null, dbTable.getCodeName());
            List tlist = DbUtil.getTables(connDb, null, null, dbTable.getCodeName());

            if (list.size() > 0) {
                wfFormPluginsMapper.deleteByModuleId(moduleId);
                for (Column field : list) {
                    String[] tempName = (field.getName()).split("_");
                    String conName = "";
                    for (Integer k = 0; k < tempName.length; k++) {
                        if (k == 0) {
                            conName = tempName[0];
                        } else {
                            conName += tempName[k].substring(0, 1).toUpperCase() + tempName[k].substring(1);
                        }
                    }

                    WfFormPlugins formPlugins = new WfFormPlugins();
                    formPlugins.setModuleId(moduleId);
                    formPlugins.setName(conName);
                    formPlugins.setNameCn(field.getRemark());
                    formPlugins.setPluginsState(1);
                    this.insertSelective(formPlugins);
                }
            } else {
                return "Error";
            }
        } catch (Exception e) {
            return "Error";
        }

        return "OK";
    }

    //获取html
    private String getHTMLContent(WfFormPlugins formPlugins) {
        String html = "<input name=\"" + formPlugins.getName() + "\" type=\"text\" cn-name=\"" + formPlugins.getNameCn() + "\"/>";

        return html;
    }

    public String moveUp(int id) {
        WfFormPlugins wfFormPlugins = wfFormPluginsMapper.selectByPrimaryKey(id);
        WfFormPlugins wfp = new WfFormPlugins();
        wfp.setModuleId(wfFormPlugins.getModuleId());
        wfp.setPluginsState(1);
        List<WfFormPluginsVO> list = wfFormPluginsMapper.selectVOAll(wfp);

        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getFpId() == wfFormPlugins.getFpId() || list.get(i).getFpId().equals(wfFormPlugins.getFpId())) {
                    index = i;
                }
            }
        }

        if (index == 0) {
            return "已到达顶部";
        } else {
            WfFormPlugins wfFormPlugins1 = list.get(index - 1);
            int sort = wfFormPlugins.getOrderNum();
            int sort1 = wfFormPlugins1.getOrderNum();

            wfFormPlugins.setOrderNum(sort1);
            wfFormPluginsMapper.updateByPrimaryKeySelective(wfFormPlugins);

            wfFormPlugins1.setOrderNum(sort);
            wfFormPluginsMapper.updateByPrimaryKeySelective(wfFormPlugins1);
        }

        return "上移成功";
    }


    public String moveDown(int id) {
        WfFormPlugins wfFormPlugins = wfFormPluginsMapper.selectByPrimaryKey(id);
        WfFormPlugins wfp = new WfFormPlugins();
        wfp.setModuleId(wfFormPlugins.getModuleId());
        wfp.setPluginsState(1);
        List<WfFormPluginsVO> list = wfFormPluginsMapper.selectVOAll(wfp);

        int index = -1;
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                if (list.get(i).getFpId() == wfFormPlugins.getFpId() || list.get(i).getFpId().equals(wfFormPlugins.getFpId())) {
                    index = i;
                }
            }
        }

        if (index == (list.size() - 1)) {
            return "已到达底部";
        } else {
            WfFormPlugins wfFormPlugins1 = list.get(index + 1);
            int sort = wfFormPlugins.getOrderNum();
            int sort1 = wfFormPlugins1.getOrderNum();

            wfFormPlugins.setOrderNum(sort1);
            wfFormPluginsMapper.updateByPrimaryKeySelective(wfFormPlugins);

            wfFormPlugins1.setOrderNum(sort);
            wfFormPluginsMapper.updateByPrimaryKeySelective(wfFormPlugins1);
        }

        return "下移成功";
    }

}
